import pymysql

def main():
    # 1.创建数据库连接
    conn = pymysql.connect(host='192.168.221.137', port=3306,user='root',password='123456',
                           db='test',charset='utf8')
    try:
        # 2.返回游标对象，通过游标对象执行SQL语句，返回处理结果
        with conn.cursor() as cursor:
            # 3.执行SQL得到结果
            result = cursor.execute('show tables')
            if result == 0:
                print('没有表')
            # 4.操作成功执行提交
            conn.commit()
    except pymysql.MySQLError as error:
        print(error)
        # 5.执行失败执行回滚
        conn.rollback()
    finally:
        # 5.关闭连接释放资源
        conn.commit()

if __name__ == '__main__':
    main()